
<!-- saved from url=(0039)http://cs.gmu.edu/~dnord/cs440/program1 -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>CS 440 Program 1</title>
</head>
<body bgcolor="ffffff"><font size="4">



<center><h2>CS 440
<br>Program 1</h2></center>
<h3><center>due September 16, 2011
</center></h3>
<h3>The Program</h3>

For the semester project you will write a compiler which generates PVM
code.  Since PVM machines do not exist you will need a PVM
<em>emulator</em> to run programs generated by your compiler.  For
Program 1 you will write this emulator.

<p>PVM emulates memory with two integer arrays, a <em>code store</em>
array for instructions and a <em>data store</em> array for data.  It
has an <em>instruction pointer</em> IP which is emulated by an integer
variable and a <em>register array</em> (an integer array) of three
registers.  There is an <em>instruction set</em>, each instruction
being three integers:  an opcode and two arguments.  

</p><p>PVM is described in detail in the PVM Definition, at
<a href="http://cs.gmu.edu/~dnord/pvm.html">
http://cs.gmu.edu/~dnord/pvm.html</a>.

</p><p>Your program will begin by getting the name of an input file (it
can either accept this file name as a command line argument or prompt
the user) and open the file.  It will allocate the code store and data
store arrays dynamically.  Choose a reasonably large size, say
several thousand integers, for each array.  The program will read the
input file into the code store array, display a message indicating
that PVM is running (e.g. "Begin PVM"), execute the loaded program,
display a message that the PVM has finished, and exit.

</p><p>Some changes and additions to this program will be made in a later
programming assignment.

</p><h3>Executing the Instructions</h3>

<p>The central part of your emulator is the <em>fetch-execute
cycle</em> which will account for most of the execution time
when your program runs.  The fetch-execute cycle is described in the
PVM Definition.  Your program <em>must</em> execute the steps in the
cycle in the order specified there.

</p><p>In the <em>execute</em> step your program will have the opcode and
arguments of an instruction and will select and execute that
instruction.  It must be done in the following manner.

</p><h4>For C programmers</h4>

<p>You will write a function for each of the 36 instructions and you
will create an array, <em>indexed by opcodes</em>, of pointers to
functions .  To select and execute an instruction you will use its
opcode as an index into this array and execute the function through
the pointer at this array position.  This can be done with one line of
code.

</p><p>Unfortunately the C syntax for an array of pointers to functions
is somewhat convoluted.  To simplify this you can use a
<em>typedef</em> to define a new type.  To define a type "pointer to
function accepting two integer parameters and with no return value"
called, say, <em>fcnptr</em> write

</p><blockquote><pre>typedef void (*fcnptr)(int, int);
</pre></blockquote>

You can then create an array of <em>fcnptr</em>s and initialize it
with pointers to the functions you have written for the instructions.

<p>The syntax for a pointer to a function is just the function's name
(without the parentheses).  To call a function through a pointer just
use the pointer in place of the function's name.  E.g. if ptr is a
pointer to a function taking two parameters you can call this function
with 

<blockquote><pre>ptr(param1, param2);
</pre></blockquote>
</p>

<h4>For Java programmers</h4>

<p>
Define an interface:
<blockquote><pre><tt>
public interface Instruction
{
   void instr(int a, int b);
}
</tt></pre></blockquote>

Define 36 classes implementing <tt>Instruction</tt>, one for each PVM 
instruction and supply <tt>instr()</tt> methods which implement the various
PVM instructions.  You can then create an array of <tt>Instruction</tt>, 
create an instance of each of these classes, and
assign references to them in the array <i>indexed by the opcodes of
the corresponding PVM instructions</i>.
</p>

<h3>To Hand In</h3>

For this assignment you will hand in two things: a source listing and a
sample terminal session.

<p>For the sample terminal session you will run your program with
sample input data (to be supplied to you) and capture the session with
the <em>script</em> utility (Unix/Linux) or some other means.  
Print out the resulting typescript and hand it in.

</p>

<h3>A Gentle Warning</h3>

It is <strong><em>extremely</em></strong> important that this program
work correctly.  Any errors in this program will make later, and more
complex, programs nearly impossible to debug.



</font></body></html>